﻿using System;
using System.Collections.Generic;

namespace Chess.Domain
{
	internal class KingPositionDeterminant : IPositionDeterminant
    {
        private Color _Color;

        /// <summary>
        /// 
        /// </summary>
        public Color Color
        {
            get { return _Color; }
            internal set { _Color = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        public List<Position> AvailablePositions(Position currentPosition)
        {
            List<Position> results = new List<Position>();
            Position item;

            if (currentPosition.CanTranslate(1, 0, out item))
            {
                results.Add(item);
            }

            if (currentPosition.CanTranslate(1, 1, out item))
            {
                results.Add(item);
            }

            if (currentPosition.CanTranslate(1, -1, out item))
            {
                results.Add(item);
            }

            if (currentPosition.CanTranslate(0, 1, out item))
            {
                results.Add(item);
            }

            if (currentPosition.CanTranslate(0, -1, out item))
            {
                results.Add(item);
            }

            if (currentPosition.CanTranslate(-1, 0, out item))
            {
                results.Add(item);
            }

            if (currentPosition.CanTranslate(-1, 1, out item))
            {
                results.Add(item);
            }

            if (currentPosition.CanTranslate(-1, -1, out item))
            {
                results.Add(item);
            }

            if (IsInitialPosition(Color, currentPosition))
            {
                // Right Castle
                if (currentPosition.CanTranslate(0, 2, out item))
                {
                    results.Add(item);
                }

                // Left Castle
                if (currentPosition.CanTranslate(0, -2, out item))
                {
                    results.Add(item);
                }
            }

            return results;
        }


        #region " Helper Methods "

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Color"></param>
        /// <param name="currentPosition"></param>
        /// <returns></returns>
        private bool IsInitialPosition(Color color, Position currentPosition)
        {
            bool result = false;

            switch (color)
            {
                case Color.White:
                    result = currentPosition.File == File.E && currentPosition.Rank == Rank.R1;

                    break;
                case Color.Black:
                    result = currentPosition.File == File.E && currentPosition.Rank == Rank.R8;

                    break;
            }

            return result;
        } 

        #endregion
    }
}
